<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import { getExam } from "@/api/student/my/my";
import { getHistoryExam } from "@/api/student/my/my";
import { getHMS } from "@/utils/time";
import router from "@/router";

const activeNames = ref(["1"]);
const activeNames1 = ref(["1"]);
const handleChange = (val: string[]) => {
  // console.log(val);
};

let exam: any = reactive([]);
let hisexam: any = reactive([]);
onMounted(() => {
  // 考试
  getExam().then((res) => {
    exam.length = 0;
    if (res.data.msg == "请求成功") {
      exam.push(res.data.data);
      if (res.data.data.length > 0) {
        exam.push(res.data.data);
      }

      // console.log(exam);
    }
  });

  //历史考试
  getHistoryExam().then((res) => {
    hisexam.length = 0;
    if (res.data.msg == "请求成功") {
      // console.log(res);
      if (res.data.data.length > 0) {
        hisexam.push(res.data.data);
      }
    }
  });
});

// 开始考试
const startExam = (id: any) => {
  router.push({
    path: "/Eaxm",
    query: {
      id: id,
    },
  });
};
</script>
<!--我的考试-->
<template>
  <div>
    <div class="common-layout">
      <el-container>
        <el-header>
          <h3 class="Title">我的考试</h3>
        </el-header>
        <el-main class="main">
          <el-scrollbar height="300px">
            <template v-if="exam.length > 0" v-for="(item, index) in exam[0]">
              <el-collapse v-model="activeNames" @change="handleChange">
                <el-collapse-item :title="item.className" :name="index + 1">
                  <template v-for="tt in item.paperVoList">
                    <div class="box">
                      <div>
                        <span>{{ tt.name }}</span>
                        <p>考试时间:{{ getHMS(tt.startTime) }}</p>
                        <p>结束时间:{{ getHMS(tt.endTime) }}</p>
                      </div>
                      <div>
                        <el-button class="btn" plain @click="startExam(tt.id)"
                          >进入考试</el-button
                        >
                      </div>
                    </div>
                  </template>
                </el-collapse-item>
              </el-collapse>
            </template>
            <template v-if="exam.length == 0">
              <el-empty description="暂无考试" />
            </template>
          </el-scrollbar>

          <el-divider>历史考试</el-divider>

          <div class="demo-collapse">
            <template v-if="hisexam.length == 0">
              <el-empty description="暂无考试" />
            </template>
            <template v-else v-for="(item, index) in hisexam[0]">
              <el-collapse v-model="activeNames1" @change="handleChange">
                <el-collapse-item :title="item.className" :name="index">
                  <template v-for="tt in item.paperVoList">
                    <div class="box">
                      <div>
                        <span>{{ tt.name }}</span>
                        <p>xxxxxxx课程需满足30分钟课时，才可参加考试</p>
                      </div>
                      <div>
                        <el-button class="btn" plain>打印凭证</el-button>
                      </div>
                    </div>
                  </template>
                </el-collapse-item>
              </el-collapse>
            </template>
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>

<style scoped>
.Title {
  color: #23b8ff;
  font-weight: bold;
}

p {
  color: #aaaaaa;
}

.main {
  background-color: white;
}

.box {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  font-size: 13px;
}

.btn {
  width: 80px;
}
</style>
